<?php

namespace Alipay;
// 调用支付宝接口。
// Loader::import('Alipay\aop\AopClient', EXTEND_PATH);
// Loader::import('Alipay\aop\request\AlipayTradeAppPayRequest', EXTEND_PATH);
import('Alipay.aop.AopClient', EXTEND_PATH);
import('Alipay.aop.request.AlipayTradeAppPayRequest', EXTEND_PATH);

/**
 * 
 * 文件用途描述: 支付宝支付
 * @authors xiaohui
 * @date    2018-02-01 15:13:58
 */
class Alipay 
{

    private $appId;        // 支付宝APPID
    private $publicKey;    // 支付宝公钥
    private $privateKey;   // 支付宝私钥
    private $signType;     // 支付宝加密方式 RSA  RSA2

    /**
     * 初始化参数
     * @param $config
     * @param $config['app_id']      支付宝APPID
     * @param $config['public_key']  支付宝公钥
     * @param $config['private_key'] 支付宝私钥
     * @param $config['sign_type']   支付宝加密方式 RSA  RSA2
     */
    public function __construct($config)
    {
        $this->appId = isset($config['app_id']) ? $config['app_id'] : '';
        $this->publicKey = isset($config['public_key']) ? $config['public_key'] : '';
        $this->privateKey = isset($config['private_key']) ? $config['private_key'] : '';
        $this->signType = isset($config['sign_type']) ? $config['sign_type'] : 'RSA';
    }

    /**
     * [makeOrderString]
     * 函数用途描述: 获取订单加签后字符串，直接用于APP支付
     * @date:  2018-02-02
     * @author: xiaohui
     * @param: [variable]
     * @param  array      $params ['body'=>'回调参数','title'=>'支付标题','online_num'=>'订单号','money'=>'支付金额单位元']
     * @return [type]             [description]
     */
    public function makeOrderString($params = array())
    {
        if (empty($params)) {
            $reData = array(
                'status' => 0,
                'msg' => '支付参数不得为空',
                'data' => ''
            );
        } elseif (empty($params['body']) || empty($params['title']) || empty($params['online_num']) || empty($params['money'])) {
            $reData = array(
                'status' => 0,
                'msg' => '必要参数不得为空',
                'data' => ''
            );
        } else {
            $aop = new \AopClient;
            $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
            $aop->appId = $this->appId;
            $aop->rsaPrivateKey = $this->privateKey;
            $aop->format = "json";
            $aop->charset = "utf-8";
            $aop->signType = $this->signType;
            $aop->alipayrsaPublicKey = $this->publicKey;
            //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
            $request = new \AlipayTradeAppPayRequest();
            //SDK已经封装掉了公共参数，这里只需要传入业务参数
            $bizcontent = "{\"body\":\"".$params['body']."\"," 
                            . "\"subject\": \"".$params['title']."\","
                            . "\"out_trade_no\": \"".$params['online_num']."\","
                            . "\"timeout_express\": \"30m\"," 
                            . "\"total_amount\": \"".$params['money']."\","
                            . "\"product_code\":\"QUICK_MSECURITY_PAY\""
                            . "}";
            $request->setNotifyUrl("http://tfb.yq008.com/PayCallback/aliPayCallback");
            $request->setBizContent($bizcontent);
            //这里和普通的接口调用不同，使用的是sdkExecute
            $response = $aop->sdkExecute($request);
            //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义，实际打印到日志以及http传输不会有这个问题
            $orderString =  htmlspecialchars($response);//就是orderString 可以直接给客户端请求，无需再做处理。
            $reData = array(
                'status' => 1,
                'msg' => '订单加签成功',
                'data' => $orderString
            );
        }

        return $reData;
    }   

    /**
     * [callbackCheck]
     * 函数用途描述: 支付宝支付回调验证
     * @date:  2018-02-02
     * @author: xiaohui
     * @param: [variable]
     * @return [type]     [description]
     */
    public function callbackCheck()
    {
        $aop = new \AopClient;
        $aop->alipayrsaPublicKey = $this->publicKey;
        $flag = $aop->rsaCheckV1($_POST, $aop->alipayrsaPublicKey, $this->signType);
        return $flag;
    }

}